package edu.iupui.csci.mortgageplanner;


import edu.iupui.csci.mortgageplanner.util.Mortgage;
import android.os.Bundle;
import android.app.ListActivity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;

public class AmortizationYearListActivity extends ListActivity {

	@Override
	protected void onResume() {
		super.onResume();
		
		int mortgageTerm = 1;
		
		try {
			mortgageTerm = Integer.parseInt(getIntent().getStringExtra("mortgage_term"));
		} catch (Exception e) {
			
		}
		
		Mortgage m = Mortgage.getMortgage();
		String[] values = new String[mortgageTerm + 1];
		values[0] = "Year    Total Interest      Total Principal";
		boolean lastYearDisplayed = false;
		for(int i = 1; i< mortgageTerm + 1; i++) {
			int length = m.getMortgageDetails().size();
			int index = (i) * 12 -1;
			if(index >= length) {
				index = length-1;
				values[i] = "";
				if(lastYearDisplayed) {
					continue;
				}
				lastYearDisplayed = true;
			}
			String tempInterest = String.format("%1$,10.2f", m.getMortgageDetails().get(index).getTotalInterest()) ;
			String tempPrincipal = String.format("%1$,10.2f", m.getMortgageDetails().get(index).getTotalPrincipal());
			values[i] = "" + (2013 + i -1 ) + "     " + tempInterest  + "     " + tempPrincipal;
		}
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
		        android.R.layout.simple_list_item_1, values);
		setListAdapter(adapter);
		adapter.notifyDataSetChanged();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_amortization_year_list);
		// Show the Up button in the action bar.
		setupActionBar();
		
		ListView yearListView = getListView();


		yearListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
		    public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		    	if(id > 0 && id <= (Mortgage.getMortgage().getMortgageDetails().size()/12 + 1) ) {
			    	Intent intent = new Intent(getApplicationContext(),AmortizationMonthListActivity.class);
					intent.putExtra("year", ((TextView)v).getText().toString());
					intent.putExtra("yearId", id);
					startActivity(intent);
		    	}
/*		    	Toast.makeText(getApplicationContext(),
		    			 ((TextView)v).getText().toString(), Toast.LENGTH_LONG)
		    		      .show();*/
		    }
		});
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {
		try{
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}catch(Throwable e) {
			
		}

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.amortization_year_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			//NavUtils.navigateUpFromSameTask(this);
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

}
